package easy.罗马数字转整数;

import java.util.HashMap;

public class Solution {
    public int romanToInt(String s) {
        HashMap<Character, Integer> symbolValues = new HashMap<>();
        symbolValues.put('I', 1);
        symbolValues.put('V', 5);
        symbolValues.put('X', 10);
        symbolValues.put('L', 50);
        symbolValues.put('C', 100);
        symbolValues.put('D', 500);
        symbolValues.put('M', 1000);
        int number = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i == s.length() - 1) {
                char lastChar = s.charAt(i);
                int lastNumber = symbolValues.get(lastChar);
                number += lastNumber;
                return number;
            }
            char currentChar = s.charAt(i);
            char nextChar = s.charAt(i + 1);
            int currentNumber = symbolValues.get(currentChar);
            int nextNumber = symbolValues.get(nextChar);
            if (currentNumber >= nextNumber) {
                number += currentNumber;
            } else {
                number -= currentNumber;
            }
        }
        return number;
    }

    public static void main(String[] args) {
        String s = "MCMXCIV";
        Solution solution = new Solution();
        System.out.println(solution.romanToInt(s));
    }
}
